package com.gt.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.admin.domain.Hospital;
import com.gt.admin.domain.ProHospital;
import com.gt.admin.domain.ProjectInfo;
import com.gt.admin.domain.vo.ProjectInfoVO;
import com.gt.admin.mapper.HospitalMapper;
import com.gt.admin.mapper.ProHospitalMapper;
import com.gt.admin.mapper.ProjectInfoMapper;
import com.gt.admin.service.IProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 项目信息service
 */
@Service
public class ProjectInfoServiceImpl extends ServiceImpl<ProjectInfoMapper, ProjectInfo> implements IProjectInfoService {
    @Autowired
    private ProjectInfoMapper projectInfoMapper;
    @Autowired
    private ProHospitalMapper proHospitalMapper;
    @Autowired
    private HospitalMapper hospitalMapper;

    /**
     * 根据id查询项目信息和关联的医院列表
     */
    @Override
    public ProjectInfoVO selectById(Long id) {
        ProjectInfo projectInfo = projectInfoMapper.selectById(id);
        ProjectInfoVO projectInfoVO = BeanUtil.copyProperties(projectInfo, ProjectInfoVO.class);
        Long[] hids = proHospitalMapper.selectHids(id);
        if (hids.length>0){
            List<Hospital> hospitalList = hospitalMapper.selectBatchIds(Arrays.asList(hids));
            projectInfoVO.setHospitals(hospitalList);
        }
        return projectInfoVO;
    }

    /**
     * 新增项目信息
     */
    @Transactional
    @Override
    public boolean add(ProjectInfoVO projectInfoVO) {
        boolean flag = save(projectInfoVO);
        Long proId = projectInfoVO.getProId();
        List<Hospital> list = projectInfoVO.getHospitals();
        List<ProHospital> plist = new ArrayList<>();
        for (Hospital hospital:list) {
            ProHospital proHospital = new ProHospital();
            proHospital.setProId(proId);
            proHospital.setHid(hospital.getHid());
            plist.add(proHospital);
        }
        if (plist.size()>0) {
            proHospitalMapper.insertBatchSomeColumn(plist);

        }
        return flag;
    }


    /**
     * 修改项目信息
     */
    @Transactional
    @Override
    public boolean update(ProjectInfoVO projectInfoVO) {
        boolean flag = updateById(projectInfoVO);
        Long proId = projectInfoVO.getProId();
        LambdaQueryWrapper<ProHospital> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProHospital::getProId, proId);
        proHospitalMapper.delete(queryWrapper);
        List<Hospital> list = projectInfoVO.getHospitals();
        List<ProHospital> plist = new ArrayList<>();
        for (Hospital hospital:list) {
            ProHospital proHospital = new ProHospital();
            proHospital.setProId(proId);
            proHospital.setHid(hospital.getHid());
            plist.add(proHospital);
        }
        if (plist.size()>0) {
            proHospitalMapper.insertBatchSomeColumn(plist);

        }
        return flag;
    }

    /**
     * 删除项目信息
     * @param proId
     * @return
     */
    @Transactional
    @Override
    public int delete(Long proId) {
        int proFlag = projectInfoMapper.deleteById(proId);
        LambdaQueryWrapper<ProHospital> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ProHospital::getProId, proId);
        int phFlag = proHospitalMapper.delete(queryWrapper);
        return proFlag+phFlag;
    }
}
